package com.gt.quality.system.controller;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gt.quality.base.BaseCommonController;
import com.gt.quality.base.BaseCommonService;
import com.gt.quality.config.utils.LoginHelper;
import com.gt.quality.system.entity.SysMenu;
import com.gt.quality.system.entity.vo.TreeManagerQueryVo;
import com.gt.quality.system.entity.vo.TreeTableMenuVo;
import com.gt.quality.base.pojo.LayerListViewObject;
import com.gt.quality.base.pojo.Resp;
import com.gt.quality.system.service.SysMenuService;
import com.gt.quality.utils.CreateTreeUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 菜单权限表(SysMenu)表控制层
 *
 * @author makejava
 * @since 2026-11-25 16:30:08
 */
@RestController
@RequestMapping("sysMenu")
@Tag(name = "菜单管理")
public class SysMenuController extends BaseCommonController<SysMenu> {
    /**
     * 服务对象
     */
    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public BaseCommonService<SysMenu> getThisService() {
        return sysMenuService;
    }


    /**
     * 菜单列表 project-web-server/src/main/originalMenuData/menu_config.json
     *
     * @return Resp
     */
    @GetMapping("/tree")
    @Operation(summary = "菜单树")
    public Resp<List<JSONObject>> treeMenu() {
        return Resp.ok(sysMenuService.cacheTreeMenuQuery());
    }

    /**
     * 菜单列表 project-web-server/src/main/originalMenuData/menu_config.json
     *
     * @return Resp
     */
    @PostMapping("/treeManager")
    @Operation(summary = "菜单管理")
    public Resp<List<JSONObject>> treeManager(@RequestBody TreeManagerQueryVo params) {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(params.getAddress()), SysMenu::getComponent, params.getAddress());
        wrapper.like(StringUtils.isNotBlank(params.getIdentifying()), SysMenu::getPerms, params.getIdentifying());
        wrapper.like(StringUtils.isNotBlank(params.getName()), SysMenu::getMenuName, params.getName());
        List<SysMenu> list = sysMenuService.list(wrapper);
        List<JSONObject> jsonObjects = list.stream().map(item -> {
            TreeTableMenuVo jsonObject = new TreeTableMenuVo();
            jsonObject.setId(item.getMenuId().toString());
            jsonObject.setParentId(item.getParentId() == null ? "" : item.getParentId().toString());
            jsonObject.setRoutePath(item.getComponent());
            jsonObject.setIcon(item.getIcon());
            jsonObject.setName(item.getMenuName());
            jsonObject.setCompontPath(item.getParentId() == null ? "" : item.getComponent());
            jsonObject.setIsShow("0".equals(item.getVisible()) ? "显示" : "隐藏");
            jsonObject.setSort(item.getOrderNum().toString());
            jsonObject.setType(item.getMenuType());
            jsonObject.setPerms(item.getPerms());
            return JSONObject.from(jsonObject);
        }).toList();
        List<JSONObject> anyThingTree = CreateTreeUtils.createAnyThingTree(jsonObjects, "parentId", "id");
        // 过滤之后只剩下子集
        if (anyThingTree.isEmpty()) {
            anyThingTree = jsonObjects;
        }
        return Resp.ok(anyThingTree);
    }

    /**
     * 增加菜单
     */
    @PostMapping("/insertOrUpdate")
    @Operation(summary = "菜单新增或者修改")
    public Resp<Boolean> insertMenu(@RequestBody TreeTableMenuVo treeTableMenuVo) {
        boolean result = sysMenuService.insertOrUpdate(treeTableMenuVo);
        return result ? Resp.ok() : Resp.fail();
    }

    /**
     * 分页查询数据
     *
     * @return 所有数据
     */
    @PostMapping("/queryTree")
    @Operation(summary = "菜单树查询")
    public Resp<LayerListViewObject> queryPage(@RequestParam(value = "id", required = false) Long menuId) {
        List<SysMenu> list = sysMenuService.listByLocalCache();
        LayerListViewObject data = sysMenuService.buildTreeDeptInfo(list, menuId);
        return Resp.ok(data);
    }

    /**
     * 操作权限
     * ['sys:user:add', 'sys:user:edit', 'sys:user:delete', 'sys:user:import', 'sys:user:export']
     *
     * @return Resp
     */
    @GetMapping("/loadPermissions")
    @Operation(summary = "操作权限")
    public Resp<Set<String>> loadPermissions() {
        Set<String> rolePermission = Objects.requireNonNull(LoginHelper.getLoginUser()).getRolePermission();
        return Resp.ok(rolePermission);
    }

    @Override
    protected boolean isSuperExcel() {
        return false;
    }
}
